/**
 * @author https://vue-admin-beautiful.com （不想保留author可删除）
 * @description 登录、获取用户信息、退出登录、清除accessToken逻辑，不建议修改
 */

import router from '@/router'
import Vue from 'vue'
import { getUserInfo, login, logout, GetToken } from '@/api/user'
import store from '@/store'
import { getAccessToken, removeAccessToken, setAccessToken } from '@/utils/accessToken'
import { resetRouter } from '@/router'
import { title, tokenName, tokenTableName } from '@/config'

const state = () => ({
  //获取访问令牌
  accessToken: getAccessToken(),
  //当前用户用户名
  username: '',
  //用户头像
  avatar: '',
  //用户权限列表
  permissions: [],
  uCode: '',
})
const getters = {
  accessToken: (state) => state.accessToken /* 返回state中的accessToken属性的值 */,
  username: (state) => state.username,
  avatar: (state) => state.avatar,
  permissions: (state) => state.permissions,
  uCode: (state) => state.uCode,
}
const mutations = {
  setuCode(state, uCode) {
    state.uCode = uCode
  },
  setToken(state, accessToken) {
    localStorage.setItem(tokenTableName, accessToken)
    state.accessToken = accessToken
  },
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken
    localStorage.setItem(tokenTableName, accessToken)
    setAccessToken(accessToken)
  },
  setUsername(state, username) {
    state.username = username
  },
  setAvatar(state, avatar) {
    state.avatar = avatar
  },
  setPermissions(state, permissions) {
    state.permissions = permissions
  },
}
const actions = {
  setPermissions({ commit }, permissions) {
    commit('setPermissions', permissions)
  },

  async login({ commit }, userInfo) {
    //const { data } = await login(userInfo)
    let data = ''
    let code = 0
    await login(userInfo).then((res) => {
      data = res.data
      code = Number(res.code)
    })
    const accessToken = data[tokenName]
    if (code === 1000) {
      commit('setAccessToken', accessToken)
      const hour = new Date().getHours()
      const thisTime = hour < 8 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 18 ? '下午好' : '晚上好'
      Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}！`)
      return data
    } else {
      Vue.prototype.$baseMessage(`登录接口异常，未正确返回${tokenName}...`, 'error')
    }
  },
  async getUserInfo({ commit }) {
    //获取登录权限
    /* const { data } = await getUserInfo()
    if (!data) {
      Vue.prototype.$baseMessage('验证失败，请重新登录...', 'error')
      return false
    } */
    let data = {
      permissions: ['admin'],
      avatar: 'https://i.gtimg.cn/club/item/face/img/8/15918_100.gif',
    }
    let { permissions, avatar } = data
    if (permissions && Array.isArray(permissions)) {
      commit('setPermissions', permissions)
      commit('setAvatar', avatar)
      return permissions
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async logout({ dispatch }) {
    // await logout(state.accessToken)//退出登录
    await dispatch('resetAccessToken') //触发 resetAccessToken重置用户的访问令牌
    await resetRouter() //重置路由恢复到初始状态（重新加载当前页面）
  },
  resetAccessToken({ commit }) {
    commit('setPermissions', []) //权限列表设置为空数组
    commit('setAccessToken', '') //访问令牌设置为空字符串
    removeAccessToken() //删除访问令牌
  },
}
export default { state, getters, mutations, actions }
